#ifndef WORKER_H
#define WORKER_H

#include <ChatClient>
#include <QEventLoop>
#include <QLogger>
#include <QThread>
#include <QTimer>

class Worker2 : public QObject {
  Q_OBJECT
public:
  Worker2() : QObject() {}

public slots:
  void run() {
    QLogger::Info("Worker2 started.");
    chatter = new TcpChatter("worker2");
    chatter->init();
    chatter->connectToServer("127.0.0.1", 9999);

    timer1 = new QTimer;
    timer2 = new QTimer;

    int cnt = 0;

    connect(timer1, &QTimer::timeout, this,
            [this]() { chatter->sendTo("Mesg", "worker1"); });
    connect(timer2, &QTimer::timeout, this, [this]() {
      QStringList s;
      s << QString::number(this->cnt) << "to";
      this->cnt += 1;
      chatter->sendCommandTo("Cmd", s, "worker1");
    });

    timer1->start(1000);
    timer2->start(2000);
  }

private:
  QTimer *timer1;
  QTimer *timer2;
  TcpChatter *chatter;
  int cnt = 0;
};

class Worker1 : public QObject {
  Q_OBJECT
public:
  Worker1() : QObject() {}

public slots:
  void run() {
    QLogger::Info("Worker1 started.");
    chatter = new TcpChatter("worker1");
    chatter->init();
    chatter->connectToServer("127.0.0.1", 9999);

    chatter->regMesgCallback(
        "Mesg", []() { QLogger::Info("This is worker1, I get a message"); });
    chatter->regCmdCallback("Cmd", [](const QStringList &lists) {
      QLogger::Info("This is worker1 I got a command, parameters:");
      foreach (auto str, lists) { QLogger::Info(str); }
    });
  }

private:
  TcpChatter *chatter;
};

#endif // WORKER_H
